package com.example.kill.controller;

import com.alibaba.druid.util.StringUtils;
import com.example.kill.pojo.KillUser;
import com.example.kill.redis.GoodsKey;
import com.example.kill.redis.RedisService;
import com.example.kill.result.Result;
import com.example.kill.service.GoodsService;
import com.example.kill.util.SpringWebContextUtil;
import com.example.kill.vo.GoodsDetailVo;
import com.example.kill.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    private static Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);


    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RedisService redisService;

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @Autowired
    ApplicationContext applicationContext;




    @RequestMapping(value = "/to_list",produces = "text/html")
    @ResponseBody
    public String list(HttpServletRequest request, HttpServletResponse response,Model model, KillUser user){
        model.addAttribute("user",user);
        List<GoodsVo> goodsList =  goodsService.getGoodsVoList();
        model.addAttribute("goodsList",goodsList);
        //return "goods_list";

        //静态化页面
        //1取缓存
        String html =  redisService.get(GoodsKey.getGoodsList,"",String.class);
        if (!StringUtils.isEmpty(html)){
            return html;
        }else{
            //手动渲染
            SpringWebContextUtil swc = new SpringWebContextUtil(request,response,request.getServletContext(),
                     request.getLocale(),model.asMap(),applicationContext);

           html =  thymeleafViewResolver.getTemplateEngine().process("goods_list",swc);
           if (!StringUtils.isEmpty(html)){
               redisService.set(GoodsKey.getGoodsList,"",html);
           }

        }
        return html;

    }

    @RequestMapping(value = "/to_detail2/{goodsId}",produces = "text/html")
    @ResponseBody
    public String to_detail2(HttpServletRequest request, HttpServletResponse response,
                            Model model, KillUser user, @PathVariable("goodsId") long goodsId){
        model.addAttribute("user",user);

        //1取缓存
        String html =  redisService.get(GoodsKey.getGoodsDetail,""+goodsId,String.class);
        if (!StringUtils.isEmpty(html)){
            return html;
        }


        GoodsVo goodsVo =  goodsService.getGoodsVoById(goodsId);

        long startAt =    goodsVo.getStartDate().getTime();
        long endAt =   goodsVo.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int  killStatus  = 0 ;
        int remainSeconds = 0 ;

        if (now<startAt){//秒杀还没开始
            killStatus = 0 ;
            remainSeconds  = (int)((startAt - now)/1000) ;
        }else if(now > endAt ) { // 秒杀已经结束
            remainSeconds = -1;
            killStatus = 2;

        }else{// 活动正在进行中
            remainSeconds = 0;
            killStatus = 1 ;
        }

        model.addAttribute("killStatus",killStatus);
        model.addAttribute("remainSeconds",remainSeconds);
        model.addAttribute("goods",goodsVo);

        //手动渲染
        SpringWebContextUtil swc = new SpringWebContextUtil(request,response,request.getServletContext(),
                request.getLocale(),model.asMap(),applicationContext);

        html =  thymeleafViewResolver.getTemplateEngine().process("goods_detail",swc);
        if (!StringUtils.isEmpty(html)){
            redisService.set(GoodsKey.getGoodsDetail,""+goodsId,html);
        }

       // return "goods_detail";
        return  html ;
    }

    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public Result<GoodsDetailVo> detail(HttpServletRequest request, HttpServletResponse response,
                                        Model model, KillUser user, @PathVariable("goodsId") long goodsId){

        GoodsVo goodsVo =  goodsService.getGoodsVoById(goodsId);

        long startAt =    goodsVo.getStartDate().getTime();
        long endAt =   goodsVo.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int  killStatus  = 0 ;
        int remainSeconds = 0 ;

        if (now<startAt){//秒杀还没开始
            killStatus = 0 ;
            remainSeconds  = (int)((startAt - now)/1000) ;
        }else if(now > endAt ) { // 秒杀已经结束
            remainSeconds = -1;
            killStatus = 2;

        }else{// 活动正在进行中
            remainSeconds = 0;
            killStatus = 1 ;
        }
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setGoodsVo(goodsVo);
        goodsDetailVo.setKillStatus(killStatus);
        goodsDetailVo.setRemainSeconds(remainSeconds);
        goodsDetailVo.setUser(user);
        return  Result.success(goodsDetailVo) ;
    }



}
